package com.soulkun.smarthome.ui.main.personal.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.amap.api.maps.model.LatLng
import com.soulkun.smarthome.logic.dto.FamilyRoomDTO
import com.soulkun.smarthome.logic.enum.NavigationEnum
import com.soulkun.smarthome.logic.model.FamilyModel
import com.soulkun.smarthome.logic.model.RoomModel
import com.soulkun.smarthome.logic.repository.ApiRepository
import com.soulkun.smarthome.logic.vo.RoomSelectVO

class AppMainPersonalFamilyCreateViewModel : ViewModel() {

    var navigationEnum: NavigationEnum? = null
    val familyNameLiveData = MutableLiveData<String>("")
    val familyLocationLiveData = MutableLiveData<LatLng>()

    val familyRoomList =
        ObservableArrayList<RoomSelectVO>().apply {
            addAll(
                arrayOf(
                    RoomSelectVO(ObservableField("客厅")),
                    RoomSelectVO(ObservableField("主卧")),
                    RoomSelectVO(ObservableField("次卧")),
                    RoomSelectVO(ObservableField("餐厅")),
                    RoomSelectVO(ObservableField("厨房")),
                    RoomSelectVO(ObservableField("书房"))
                )
            )
        }

    fun createFamily() = run {
        val familyRoomDTO = FamilyRoomDTO(
            FamilyModel(
                name = familyNameLiveData.value!!,
                latitude = familyLocationLiveData.value!!.latitude,
                longitude = familyLocationLiveData.value!!.longitude
            ),
            familyRoomList.filter { roomVO -> roomVO.isSelected.get() }
                .map { roomVO -> RoomModel(name = roomVO.name.get()) })
        ApiRepository.createFamily(familyRoomDTO)
    }

}